<!DOCTYPE html>
<html lang="">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta itemprop="name" content="golang中使用excelize导出表格">
<meta itemprop="description" content="golang中使用excelize导出表格 背景：客户每天定点取一组数据填入excel中，记录发酵周期内温度变化，并根据公式计算报表值，现希望通过系统完成该操作 分析 针对golang的几款兼容性好的excel包有tealeg, excelize, openxlsx等，对比语法，选择了excelize
Step1: 下载安装包 方式一： 在go.mod文件中，填写 github.com/xuri/excelize/v2 v2.6.1 ，然后执行 go mod tidy方式二：在终端输入go get github.com/xuri/excelize/v2 Step2: 查询数据 根据客户要求，查询窖池中固定时间点的一条数据，分上中下层，当设备未上报该时间点数据时，取时间点前后教近的一条，该步骤省略&hellip;
Step3: 处理表格 // 创建一个新的Excel文件f := excelize.NewFile() 注意，同一个excel单元格只能设置一次样式，多次设置会覆盖
 // 如果一个单元格的样式较多，可以把样式一次性加上styleID, _ := f.NewStyle(`{&#34;font&#34;: {&#34;size&#34;:12,&#34;bold&#34;: true},&#34;alignment&#34;: {&#34;horizontal&#34;: &#34;center&#34;,&#34;vertical&#34;: &#34;center&#34;}}`)f.SetCellStyle(&#34;Sheet1&#34;, &#34;A1&#34;, &#34;A1&#34;, styleColor) // 设置样式// 合并单元格f.MergeCell(&#34;Sheet1&#34;, &#34;A1&#34;, &#34;A2&#34;)// 设置单元格值f.SetCellValue(&#34;Sheet1&#34;, &#34;A1&#34;, OperationName)// 设置行高f.">
<meta itemprop="datePublished" content="2024-07-19T16:22:21&#43;08:00" />
<meta itemprop="dateModified" content="2024-07-19T16:22:21&#43;08:00" />
<meta itemprop="wordCount" content="195">



<meta itemprop="keywords" content="" /><meta property="og:title" content="golang中使用excelize导出表格" />
<meta property="og:description" content="golang中使用excelize导出表格 背景：客户每天定点取一组数据填入excel中，记录发酵周期内温度变化，并根据公式计算报表值，现希望通过系统完成该操作 分析 针对golang的几款兼容性好的excel包有tealeg, excelize, openxlsx等，对比语法，选择了excelize
Step1: 下载安装包 方式一： 在go.mod文件中，填写 github.com/xuri/excelize/v2 v2.6.1 ，然后执行 go mod tidy方式二：在终端输入go get github.com/xuri/excelize/v2 Step2: 查询数据 根据客户要求，查询窖池中固定时间点的一条数据，分上中下层，当设备未上报该时间点数据时，取时间点前后教近的一条，该步骤省略&hellip;
Step3: 处理表格 // 创建一个新的Excel文件f := excelize.NewFile() 注意，同一个excel单元格只能设置一次样式，多次设置会覆盖
 // 如果一个单元格的样式较多，可以把样式一次性加上styleID, _ := f.NewStyle(`{&#34;font&#34;: {&#34;size&#34;:12,&#34;bold&#34;: true},&#34;alignment&#34;: {&#34;horizontal&#34;: &#34;center&#34;,&#34;vertical&#34;: &#34;center&#34;}}`)f.SetCellStyle(&#34;Sheet1&#34;, &#34;A1&#34;, &#34;A1&#34;, styleColor) // 设置样式// 合并单元格f.MergeCell(&#34;Sheet1&#34;, &#34;A1&#34;, &#34;A2&#34;)// 设置单元格值f.SetCellValue(&#34;Sheet1&#34;, &#34;A1&#34;, OperationName)// 设置行高f." />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://www.lican.site/postsbak/golang%E7%9A%84excelize%E4%BD%BF%E7%94%A8%E5%BF%83%E5%BE%97/" />
<meta property="article:published_time" content="2024-07-19T16:22:21+08:00" />
<meta property="article:modified_time" content="2024-07-19T16:22:21+08:00" />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="golang中使用excelize导出表格"/>
<meta name="twitter:description" content="golang中使用excelize导出表格 背景：客户每天定点取一组数据填入excel中，记录发酵周期内温度变化，并根据公式计算报表值，现希望通过系统完成该操作 分析 针对golang的几款兼容性好的excel包有tealeg, excelize, openxlsx等，对比语法，选择了excelize
Step1: 下载安装包 方式一： 在go.mod文件中，填写 github.com/xuri/excelize/v2 v2.6.1 ，然后执行 go mod tidy方式二：在终端输入go get github.com/xuri/excelize/v2 Step2: 查询数据 根据客户要求，查询窖池中固定时间点的一条数据，分上中下层，当设备未上报该时间点数据时，取时间点前后教近的一条，该步骤省略&hellip;
Step3: 处理表格 // 创建一个新的Excel文件f := excelize.NewFile() 注意，同一个excel单元格只能设置一次样式，多次设置会覆盖
 // 如果一个单元格的样式较多，可以把样式一次性加上styleID, _ := f.NewStyle(`{&#34;font&#34;: {&#34;size&#34;:12,&#34;bold&#34;: true},&#34;alignment&#34;: {&#34;horizontal&#34;: &#34;center&#34;,&#34;vertical&#34;: &#34;center&#34;}}`)f.SetCellStyle(&#34;Sheet1&#34;, &#34;A1&#34;, &#34;A1&#34;, styleColor) // 设置样式// 合并单元格f.MergeCell(&#34;Sheet1&#34;, &#34;A1&#34;, &#34;A2&#34;)// 设置单元格值f.SetCellValue(&#34;Sheet1&#34;, &#34;A1&#34;, OperationName)// 设置行高f."/>

	<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
	<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
	<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
	<link rel="manifest" href="/site.webmanifest">
	<link rel="mask-icon" href="/safari-pinned-tab.svg" color="">
	<link rel="shortcut icon" href="/favicon.ico">

	<title>golang中使用excelize导出表格</title>
	<link rel="stylesheet" href="https://www.lican.site/css/style.min.d3141168199607bf3a517216ce3c263814eecdbc8fca72a9a88700799a838219.css">
	
</head>

<body id="page">
	
	<header id="site-header" class="animated slideInUp faster">
		<div class="hdr-wrapper section-inner">
			<div class="hdr-left">
				<div class="site-branding">
					<a href="https://www.lican.site">花落雨忧</a>
				</div>
				<nav class="site-nav hide-in-mobile">
					<a href="https://www.lican.site/posts">首页</a>
					<a href="https://www.lican.site/tech/">技术文档</a>
					<a href="https://www.lican.site/article/">文章</a>
					<a href="https://www.lican.site/project/">项目</a>
					<a href="https://www.lican.site/about/">关于</a>
				</nav>
			</div>
			<div class="hdr-right hdr-icons">
				<button id="menu-btn" class="hdr-btn" title="Menu"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-menu"><line x1="3" y1="12" x2="21" y2="12"></line><line x1="3" y1="6" x2="21" y2="6"></line><line x1="3" y1="18" x2="21" y2="18"></line></svg></button>
			</div>
		</div>
	</header>
	<div id="mobile-menu" class="animated fast">
		<ul>
			<li><a href="https://www.lican.site/posts">首页</a></li>
			<li><a href="https://www.lican.site/tech/">技术文档</a></li>
			<li><a href="https://www.lican.site/article/">文章</a></li>
			<li><a href="https://www.lican.site/project/">项目</a></li>
			<li><a href="https://www.lican.site/about/">关于</a></li>
		</ul>
	</div>


	<main class="site-main section-inner thin animated fadeIn faster">
		<h1>golang中使用excelize导出表格</h1>
		<div class="content">
			<h3 id="golang中使用excelize导出表格">golang中使用excelize导出表格<a href="#golang中使用excelize导出表格" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h3>
<h4 id="背景客户每天定点取一组数据填入excel中记录发酵周期内温度变化并根据公式计算报表值现希望通过系统完成该操作">背景：客户每天定点取一组数据填入excel中，记录发酵周期内温度变化，并根据公式计算报表值，现希望通过系统完成该操作<a href="#背景客户每天定点取一组数据填入excel中记录发酵周期内温度变化并根据公式计算报表值现希望通过系统完成该操作" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h4>
<h4 id="分析">分析<a href="#分析" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h4>
<p>针对golang的几款兼容性好的excel包有tealeg, excelize, openxlsx等，对比语法，选择了excelize</p>
<h5 id="step1-下载安装包">Step1: 下载安装包<a href="#step1-下载安装包" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h5>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">方式一</span><span style="color:#960050;background-color:#1e0010">：</span> <span style="color:#a6e22e">在go</span>.<span style="color:#a6e22e">mod文件中</span><span style="color:#960050;background-color:#1e0010">，</span><span style="color:#a6e22e">填写</span> <span style="color:#a6e22e">github</span>.<span style="color:#a6e22e">com</span><span style="color:#f92672">/</span><span style="color:#a6e22e">xuri</span><span style="color:#f92672">/</span><span style="color:#a6e22e">excelize</span><span style="color:#f92672">/</span><span style="color:#a6e22e">v2</span> <span style="color:#a6e22e">v2</span><span style="color:#ae81ff">.6.1</span> <span style="color:#960050;background-color:#1e0010">，</span><span style="color:#a6e22e">然后执行</span> <span style="color:#66d9ef">go</span> <span style="color:#a6e22e">mod</span> <span style="color:#a6e22e">tidy</span>
<span style="color:#a6e22e">方式二</span><span style="color:#960050;background-color:#1e0010">：</span><span style="color:#a6e22e">在终端输入go</span> <span style="color:#a6e22e">get</span> <span style="color:#a6e22e">github</span>.<span style="color:#a6e22e">com</span><span style="color:#f92672">/</span><span style="color:#a6e22e">xuri</span><span style="color:#f92672">/</span><span style="color:#a6e22e">excelize</span><span style="color:#f92672">/</span><span style="color:#a6e22e">v2</span> 
</code></pre></div><h5 id="step2-查询数据">Step2: 查询数据<a href="#step2-查询数据" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h5>
<p>根据客户要求，查询窖池中固定时间点的一条数据，分上中下层，当设备未上报该时间点数据时，取时间点前后教近的一条，该步骤省略&hellip;</p>
<h5 id="step3-处理表格">Step3: 处理表格<a href="#step3-处理表格" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h5>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 创建一个新的Excel文件
</span><span style="color:#75715e"></span><span style="color:#a6e22e">f</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">excelize</span>.<span style="color:#a6e22e">NewFile</span>()
</code></pre></div><blockquote>
<p>注意，同一个excel单元格只能设置一次样式，多次设置会覆盖</p>
</blockquote>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 如果一个单元格的样式较多，可以把样式一次性加上
</span><span style="color:#75715e"></span><span style="color:#a6e22e">styleID</span>, <span style="color:#a6e22e">_</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">f</span>.<span style="color:#a6e22e">NewStyle</span>(<span style="color:#e6db74">`{
</span><span style="color:#e6db74">		&#34;font&#34;: {
</span><span style="color:#e6db74">			&#34;size&#34;:12,
</span><span style="color:#e6db74">			&#34;bold&#34;: true
</span><span style="color:#e6db74">		},
</span><span style="color:#e6db74">		&#34;alignment&#34;: {
</span><span style="color:#e6db74">			&#34;horizontal&#34;: &#34;center&#34;,
</span><span style="color:#e6db74">			&#34;vertical&#34;: &#34;center&#34;
</span><span style="color:#e6db74">		}
</span><span style="color:#e6db74">	}`</span>)
<span style="color:#a6e22e">f</span>.<span style="color:#a6e22e">SetCellStyle</span>(<span style="color:#e6db74">&#34;Sheet1&#34;</span>, <span style="color:#e6db74">&#34;A1&#34;</span>, <span style="color:#e6db74">&#34;A1&#34;</span>, <span style="color:#a6e22e">styleColor</span>) <span style="color:#75715e">// 设置样式
</span></code></pre></div><div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 合并单元格
</span><span style="color:#75715e"></span><span style="color:#a6e22e">f</span>.<span style="color:#a6e22e">MergeCell</span>(<span style="color:#e6db74">&#34;Sheet1&#34;</span>, <span style="color:#e6db74">&#34;A1&#34;</span>, <span style="color:#e6db74">&#34;A2&#34;</span>)
<span style="color:#75715e">// 设置单元格值
</span><span style="color:#75715e"></span><span style="color:#a6e22e">f</span>.<span style="color:#a6e22e">SetCellValue</span>(<span style="color:#e6db74">&#34;Sheet1&#34;</span>, <span style="color:#e6db74">&#34;A1&#34;</span>, <span style="color:#a6e22e">OperationName</span>)
<span style="color:#75715e">// 设置行高
</span><span style="color:#75715e"></span><span style="color:#a6e22e">f</span>.<span style="color:#a6e22e">SetRowHeight</span>(<span style="color:#e6db74">&#34;Sheet1&#34;</span>, <span style="color:#e6db74">&#34;A1&#34;</span>, <span style="color:#ae81ff">22.0</span>)
</code></pre></div><div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 表格的填充需根据excel的横纵两个轴循环写入 eg:
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">columnIndex</span> = <span style="color:#ae81ff">2</span>
	<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">device</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">data</span> { <span style="color:#75715e">// 循环填入数据
</span><span style="color:#75715e"></span>		<span style="color:#a6e22e">rowIndex</span> <span style="color:#f92672">:=</span> <span style="color:#ae81ff">4</span>
		<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">dataGroup</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">device</span>.<span style="color:#a6e22e">Data</span> {
			<span style="color:#a6e22e">lie1</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">GetColumnLetter</span>(<span style="color:#a6e22e">columnIndex</span>)
			<span style="color:#a6e22e">lie2</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">GetColumnLetter</span>(<span style="color:#a6e22e">columnIndex</span> <span style="color:#f92672">+</span> <span style="color:#ae81ff">1</span>)
			<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">item</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">dataGroup</span> {
				<span style="color:#a6e22e">t</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">Unix</span>(<span style="color:#a6e22e">item</span>.<span style="color:#a6e22e">CreatedTime</span>, <span style="color:#ae81ff">0</span>)
				<span style="color:#a6e22e">timeStr</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">t</span>.<span style="color:#a6e22e">Format</span>(<span style="color:#e6db74">&#34;01月02日 15:04&#34;</span>)
				<span style="color:#a6e22e">f</span>.<span style="color:#a6e22e">SetCellValue</span>(<span style="color:#e6db74">&#34;Sheet1&#34;</span>, <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Sprintf</span>(<span style="color:#e6db74">&#34;%s%d&#34;</span>, <span style="color:#a6e22e">lie1</span>, <span style="color:#a6e22e">rowIndex</span>), <span style="color:#a6e22e">timeStr</span>)
				<span style="color:#a6e22e">f</span>.<span style="color:#a6e22e">SetCellValue</span>(<span style="color:#e6db74">&#34;Sheet1&#34;</span>, <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Sprintf</span>(<span style="color:#e6db74">&#34;%s%d&#34;</span>, <span style="color:#a6e22e">lie2</span>, <span style="color:#a6e22e">rowIndex</span>), <span style="color:#a6e22e">item</span>.<span style="color:#a6e22e">Value</span>)
				<span style="color:#a6e22e">f</span>.<span style="color:#a6e22e">SetCellStyle</span>(<span style="color:#e6db74">&#34;Sheet1&#34;</span>, <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Sprintf</span>(<span style="color:#e6db74">&#34;%s%d&#34;</span>, <span style="color:#a6e22e">lie2</span>, <span style="color:#a6e22e">rowIndex</span>), <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Sprintf</span>(<span style="color:#e6db74">&#34;%s%d&#34;</span>, <span style="color:#a6e22e">lie2</span>, <span style="color:#a6e22e">rowIndex</span>), <span style="color:#a6e22e">styleCenter</span>)
			}
			<span style="color:#a6e22e">rowIndex</span><span style="color:#f92672">++</span>
		}
		<span style="color:#a6e22e">columnIndex</span> <span style="color:#f92672">+=</span> <span style="color:#ae81ff">2</span> <span style="color:#75715e">// 移动到下一列
</span><span style="color:#75715e"></span>	}
</code></pre></div><h5 id="step3-保存文件">Step3: 保存文件<a href="#step3-保存文件" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h5>
<p>excel表格数据填充完毕后，保存文件</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// 保存文件
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">FileName</span> <span style="color:#f92672">:=</span> <span style="color:#e6db74">&#34;langjiu_pit.xlsx&#34;</span>
	<span style="color:#a6e22e">f</span>.<span style="color:#a6e22e">SaveAs</span>(<span style="color:#a6e22e">FileName</span>)
<span style="color:#75715e">// 执行后，可看到根目录有一个名为langjiu_pit.xlsx的表格
</span></code></pre></div><h5 id="step4-导出表格">Step4: 导出表格<a href="#step4-导出表格" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h5>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// // 设置HTTP响应头，指定文件名和Content-Type
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">co</span>.<span style="color:#a6e22e">Header</span>(<span style="color:#e6db74">&#34;Content-Disposition&#34;</span>, <span style="color:#e6db74">&#34;attachment; filename=&#34;</span><span style="color:#f92672">+</span><span style="color:#a6e22e">FileName</span>)
	<span style="color:#a6e22e">co</span>.<span style="color:#a6e22e">Header</span>(<span style="color:#e6db74">&#34;Content-Type&#34;</span>, <span style="color:#e6db74">&#34;application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&#34;</span>)
	<span style="color:#75715e">// 打开文件
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">file</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">os</span>.<span style="color:#a6e22e">Open</span>(<span style="color:#a6e22e">FileName</span>) <span style="color:#75715e">// 替换为你的文件路径
</span><span style="color:#75715e"></span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
		<span style="color:#66d9ef">return</span>
	}
	<span style="color:#66d9ef">defer</span> <span style="color:#a6e22e">file</span>.<span style="color:#a6e22e">Close</span>()

	<span style="color:#75715e">// 发送文件内容
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">err</span> = <span style="color:#a6e22e">io</span>.<span style="color:#a6e22e">Copy</span>(<span style="color:#a6e22e">co</span>.<span style="color:#a6e22e">Writer</span>, <span style="color:#a6e22e">file</span>) <span style="color:#75715e">// 注意：这里使用了io.Copy，但没有在示例中导入&#34;io&#34;包
</span></code></pre></div><h5 id="step5-查看表格">Step5: 查看表格<a href="#step5-查看表格" class="anchor" aria-hidden="true"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg></a></h5>
<p><img src="https://www.lican.site/images/1721377152631.png" alt="1721377152631"></p>

		</div>
	</main>

	<footer id="site-footer" class="section-inner thin animated fadeIn faster">
		<p>&copy; 2024 <a href="https://www.lican.site"></a></p>
		<p>
			Made with <a href="https://gohugo.io/" target="_blank" rel="noopener">Hugo</a> &#183; Theme <a href="https://github.com/Track3/hermit" target="_blank" rel="noopener">Hermit</a> &#183; <a href="https://www.lican.site/post/index.xml" target="_blank" title="rss"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-rss"><path d="M4 11a9 9 0 0 1 9 9"></path><path d="M4 4a16 16 0 0 1 16 16"></path><circle cx="5" cy="19" r="1"></circle></svg></a>
		</p>
	</footer>


	<script src="https://www.lican.site/js/main.min.784417f5847151f848c339cf0acb13a06cbb648b1483435a28ed4556c4ead69b.js"></script>

</body>

</html>
